#include <bits/stdc++.h>
using namespace std;
using ll=long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }

        vector<ll> mval(n + 1, LLONG_MAX);
        int i = 0;
        while (i < n) {
            int j = i;
            while (j < n && a[j] == a[i]) {
                j++;
            }
            int x = a[i];
            ll cost;

            if (i == 0 && j == n) {
                cost = 0;
            } else if (i == 0) {
                cost = 1LL * (n - j) * x;
            } else if (j == n) {
                cost = 1LL * i * x;
            } else {
                cost = 1LL * (i + n - j) * x;
            }

            if (cost < mval[x]) {
                mval[x] = cost;
            }

            i = j;
        }

        ll ans = LLONG_MAX;
        for (int x = 1; x <= n; x++) {
            if (mval[x] < ans) {
                ans = mval[x];
            }
        }

        cout << ans << '\n';
    }

    return 0;
}
